<?php
!function_exists('adminmsg') && exit('Forbidden');
$housefieldsService = House::loadService('HouseFieldsService');
$plateField = $housefieldsService->getCompsiteFieldsByType('plate');//所在商圈
$brokerService = House::loadService('brokerService');
$type = $type ? $type : 'manager';
$nav[$type] = "class = 'current'";
$basename .= '&type='.$type;
$perpage = 20;
if (!$action) {
	InitGP(array('area','realname','username','page','groupid'));
	$url = $basename."&groupid=$groupid&realname=$realname&username=$username&";
	$brokerGroupService = House::loadService('brokerGroupService');
	$brokerGroup = $brokerGroupService->getBrokerGroups();
	$where = array(
				'username' => $username,
				'realname' => $realname,
				'area'     => $area,
				'groupid'  => $groupid,
			);
	$return = $brokerService->findBrokers($where,$page,$perpage);
	list($brokerList,$count) = $return;
	$totalCount = $count ? $count : 0;
	$pageCount = ceil($count / $perpage);
	$page = validatePage($page,$pageCount);
	$lastUrl = $url . "page=$page";
	$pages = numofpage($count, $page, $pageCount,$url);
	$ajaxUrl = EncodeUrl($basename);
	require_once PrintMode('broker.search');
} elseif ($action == 'edit') {
	InitGP(array('username','groupid','realname','ifactive','ifmsg','area','plate','company','address','email','tel','phone','_verify','idcard','alipay','otherinfo','remark','uid','step'));
	$brokerModule = $brokerService->getBrokerModule($uid);
	if ($step==2) {
		$url = $basename.'&action=edit&uid='.$uid;
		$brokerModule->setRealname($realname);
		$brokerModule->setGroupid($groupid);
		$brokerModule->setIfactive($ifactive);
		$brokerModule->setArea($area);
		$brokerModule->setPlate($plate);
		$brokerModule->setCompany($company);
		$brokerModule->setAddress($address);
		$brokerModule->setEmail($email);
		$brokerModule->setTel($tel);
		$brokerModule->setPhone($phone);
		$brokerModule->setVerify($_verify);
		$brokerModule->setIdcard($idcard);
		$brokerModule->setIdcardpic();
		$brokerModule->setAlipay($alipay);
		$brokerModule->setOtherinfo($otherinfo);
		$brokerModule->setRemark($remark);
		$brokerModule->checkTelOrPhone();
		Error::checkError($url);
		$brokerModule->setIfmsg($ifmsg);
		$brokerService->editBroker($brokerModule);
		adminmsg('operate_success',$url);
	} else {
		$readonly = "readonly=readonly";
		$title = "编辑经纪人";
		$brokerGroupService = House::loadService('brokerGroupService');
		$groups = $brokerGroupService->getBrokerGroups();
		ifcheck($brokerModule->ifactive, 'ifactive');
		ifcheck(0, 'ifmsg');
		require_once PrintMode('broker.edit');
	}
} elseif ($action == 'add') {
	InitGP(array('username','groupid','realname','ifactive','ifmsg','area','plate','company','address','email','tel','phone','_verify','idcard','alipay','otherinfo','remark','step'));
	$brokerModule = $brokerService->getBrokerModule();
	$url = $basename.'&action=add';
	$jumpurl =  $url.'&username='.$username.'&realname='.$realname.'&company='.$company.'&address='.$address.'&tel='.$tel.'&phone='.$phone.'&idcard='.$idcard.'&alipay='.$alipay.'&otherinfo='.$otherinfo.'&otherinfo='.$otherinfo.'&ifactive_y='.$ifactive_y.'&ifmsg_y='.$ifmsg_y.'_verify='.$_verify;
	$brokerModule->setUsername($username);
	$brokerModule->setRealname($realname);
	$brokerModule->setGroupid($groupid);
	$brokerModule->setIfactive($ifactive);
	$brokerModule->setArea($area);
	$brokerModule->setPlate($plate);
	$brokerModule->setCompany($company);
	$brokerModule->setAddress($address);
	$brokerModule->setEmail($email);
	$brokerModule->setTel($tel);
	$brokerModule->setPhone($phone);
	$brokerModule->setVerify($_verify);
	$brokerModule->setIdcard($idcard);
	$brokerModule->setIdcardpic();
	$brokerModule->setAlipay($alipay);
	$brokerModule->setOtherinfo($otherinfo);
	$brokerModule->setRemark($remark);
	$brokerModule->setCreatetime($timestamp);
	$brokerModule->checkTelOrPhone();
	$brokerModule->checkIfBroker();
	if ($step==2) {
		Error::checkError($jumpurl);
		$brokerModule->setIfmsg($ifmsg);
		$brokerService->addBroker($brokerModule);
		adminmsg('operate_success',$url);
	} else {
		$title = "添加经纪人";
		$brokerGroupService = House::loadService('brokerGroupService');
		$groups = $brokerGroupService->getBrokerGroups();
		ifcheck(1, 'ifactive');
		ifcheck(1, 'ifmsg');
		require_once PrintMode('broker.edit');
	}
} elseif ($action =='option') {
	InitGP(array('uids','optiontype','lastUrl', 'groupid', 'realname', 'username', 'page'));
	!$lastUrl && $lastUrl = $basename."&groupid=$groupid&realname=$realname&username=$username&page=$page";
	if($uids) $uids = is_array($uids) ? $uids : array($uids);
	if($optiontype == 'delete'){
		!$uids && adminmsg('请选择您要删除的经纪人');
		$brokerService->deleteBrokers($uids);
	}elseif($optiontype == 'active'){
		!$uids && adminmsg('请选择要激活的经纪人');
		$brokerService->updateBrokers(array('ifactive'=>'1'),$uids);
	}elseif($optiontype == 'lock'){
		!$uids && adminmsg('请选择您要锁定的经纪人');
		$brokerService->updateBrokers(array('ifactive'=>'0'),$uids);
	}
	adminmsg('operate_success', $lastUrl);
}
?>